<template>
  <view class="container">
    <activity :activityList="list" activityType="course" pageType="goods"></activity>
    <emptyData
      :width="empty.width"
      :height="empty.height"
      :text="empty.text"
      :icon="empty.icon"
      :marginTop="empty.marginTop"
      v-if="list.length == 0"
    ></emptyData>
  </view>
</template>

<script>
import activity from "../../components/activity/activity.vue";
export default {
  components: {
    activity,
  },
  data() {
    return {
      page: 1,
      list: [],
      empty: {
        width: 135,
        height: 134,
        text: "暂无数据",
        icon: "/image/emptyAddress.png",
        marginTop: 100,
      },
      category_id: 0,
    };
  },
  onLoad(e) {
    this.isIphoneX = this.$util.uniappIsIPhoneX();
    if (e.category_id) {
      this.category_id = e.category_id;
    }

    if (e.category_name) {
      uni.setNavigationBarTitle({
        title: e.category_name,
      });
    }
    this.getList();
  },
  onReachBottom() {
    this.page++;
    this.getList();
  },
  methods: {
    getList() {
      this.$api.sendRequest({
        url: "/api/child/goodslist",
        data: {
          page: this.page,
          page_size: 20,
          category_id: this.category_id,
        },
        success: (res) => {
          if (res.code == 0) {
            if (this.page == 1) this.list = [];
            this.list = [...this.list, ...res.data.list];

            // 循环数组，格式化开始时间和结束时间格式为08:00
            for (let i = 0; i < this.list.length; i++) {
              this.list[i].start_time = this.formatTime(this.list[i].start_time, "HH:mm");
              this.list[i].end_time = this.formatTime(this.list[i].end_time, "HH:mm");
            }

            // 如果商品图片存在且为多张的情况下，取第一张图片作为封面图，已逗号拆分字符串，第一张图片做为封面图
            for (let i = 0; i < this.list.length; i++) {
              if (this.list[i].goods_image) {
                var images = this.list[i].goods_image.split(",");
                this.list[i].goods_image = images[0];
              }

              if (this.list[i].goods_label) {
                // 判断里面是否有逗号，如果有就分割成数组
                if (this.list[i].goods_label.indexOf(",") != -1)
                  this.list[i].goods_label = this.list[i].goods_label.split(",");
                else this.list[i].goods_label = [this.list[i].goods_label];
              } else this.list[i].goods_label = [];
            }
          }
        },
      });
    },
    formatTime(time) {
      //    time的格式是12:00:00
      return time.substring(0, 5);
    },
  },
};
</script>

<style lang="scss" scoped></style>
